package cn.antsing;

import cn.antsing.config.BeanConfiguration;
import cn.antsing.entity.User;
import cn.antsing.factoryBean.UserFactoryBean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfiguration.class);

        //最普通的方式获取user 这个bean
        User user = (User) applicationContext.getBean("user");
        System.out.println("最普通的方式获取user 这个bean:"+user);
        user = (User) applicationContext.getBean("user");
        System.out.println("最普通的方式获取user 这个bean:"+user);
        System.out.println("===========================分割线===================================");
        //通过factoryBean方式获取User这个Bean
        User user1 = (User) applicationContext.getBean("getUserFactoryBean");
        System.out.println("通过factoryBean方式获取User这个Bean:"+user1);
        user1 = (User) applicationContext.getBean("getUserFactoryBean");
        System.out.println("通过factoryBean方式获取User这个Bean:"+user1);

        System.out.println("===========================分割线===================================");

        //获取UserFactoryBean本身这个bean
        System.out.println("获取UserFactoryBean本身这个bean:"+applicationContext.getBean(UserFactoryBean.class));
        System.out.println("获取UserFactoryBean本身这个bean:"+applicationContext.getBean("&getUserFactoryBean"));
        UserFactoryBean userFactoryBean = applicationContext.getBean(UserFactoryBean.class);
        User user2 = userFactoryBean.getObject();
        System.out.println(user2);

        System.out.println("===========================分割线===================================");

        //通过UserFactory方式获取user这个bean
        System.out.println("通过UserFactory方式获取user这个bean:"+applicationContext.getBean("getUserByUserFactory"));

        /**
         * 有下面这行代码，DisposableBean接口的方法才会执行
         */
       applicationContext.close();
    }
}
